use Maze;
use feature 'say';
use Term::Readkey;

my $cols = 13;
my $rows = 13;
my $start = 0;
my $goal = int rand($cols * $rows);
my $limit = 3;

my $maze = Maze->new(cols => $cols, rows => $rows, goal => $goal, start => $start);
$maze->buildMaze($limit);
#$maze->printMaze();
$maze->makeConnected();
$maze->makeConnected();
#$maze->printMaze();
#say $maze->getConnectedComponents(format => 'string');
#print "\n";
#say $maze->getGoalState(coords=>1);
#my @components = $maze->getConnectedComponents(format=>'list');
#for my $component (@components) {
#    say "(".join(", ", sort{$a<=>$b} @$component).")";    }

#say $maze->getPosition(1);
#while (1) {
#    my $input = <>;
#    chomp $input;
#    if ($input =~ m/^q$/) {
#        last;    }
#    next if $input !~ m/^\d+$/;
#    say $maze->getPosition($input);    }
#    #my ($v1, $v2) = split(':', $input);
#    #say $maze->getGoalState(),"\t",$maze->getGoalState(coords=>1);
#    #print "hasEdge($v1, $v2) = '".($maze->hasEdge($v1, $v2))."'\n" if (defined $v1 && defined $v2);    }

#+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*#

use Term::ReadKey;
use Term::Animation;
use Curses;


my $maze_shape = $maze->getMazeString();
my $maze_animation = Term::Animation->new();
$maze_animation->new_entity(
                        name        => 'Maze',
                        shape       => $maze_shape,
                        position    => [0, 0, 0],
                    );
my $pacman = 'o';
my $pacman_entity = $maze_animation->new_entity(
                        name            => 'Pacman',
                        shape           => $pacman,
                        position        => [1, 2, 0],
                        callback_args   => [0, 0, 0, 0],
                    );

my $dispatchTable = { a => [-3,0,0,0],
                      d => [3,0,0,0],
                      w => [0,-2,0,0],
                      s => [0,2,0,0],
                    };

my ($col, $row) = (0, 0);
my $transformTable = { a => \&decrement(\$col),
                       s => \&increment(\$row),
                       d => \&increment(\$col),
                       w => \&decrement(\$row),
                     };
sub increment { my $var = shift; $$var++; };
sub decrement { my $var = shift; $$var--; };

halfdelay(5);
ReadMode('cbreak');
while (1) {
    $maze_animation->animate();
    my $input = ReadKey(0);
    if ($input eq 'q') {
        last;    }
    elsif ($input =~ m/a|s|d|w/) {
        $pacman_entity->callback_args($dispatchTable->{$input});    }
    else {
        $pacman_entity->callback_args([0,0,0,0]);    }  }

ReadMode ('normal');

